home *** CD-ROM | disk | FTP | other *** search
- #include "stdafx.h"
-
- #include <stdarg.h>
-
- char *construct(const char *fn, ...)
- {
- static char s[512];
- va_list arg;
-
- va_start(arg, fn);
- vsprintf(s, fn, arg);
-
- return s;
- }
-
- int file_size(const char *fn)
- {
- TRY
- {
- CFile f(fn, CFile::modeRead | CFile::shareDenyNone);
-
- return f.GetLength();
- }
- CATCH(CFileException, e)
- {
- error("Unable to open %s", fn);
- }
- END_CATCH
-
- return 0;
- }
-
- char *read_file(const char *fn, int *size)
- {
- TRY
- {
- CFile f(fn, CFile::modeRead | CFile::shareDenyNone);
-
- int length = f.GetLength();
- char *data = new char[length];
-
- f.Read(data, length);
-
- if (size != 0)
- *size = length;
-
- return data;
- }
- CATCH(CFileException, e)
- {
- error("Unable to open %s", fn);
- }
- END_CATCH
-
- return 0;
- }
-
- char *read_part_file(const char *fn, int start, int length)
- {
- TRY
- {
- CFile f(fn, CFile::modeRead | CFile::shareDenyNone);
-
- char *data = new char[length];
-
- f.Seek(start, CFile::begin);
- f.Read(data, length);
-
- return data;
- }
- CATCH(CFileException, e)
- {
- error("Unable to open %s", fn);
- }
- END_CATCH
-
- return 0;
- }
-
- void write_file(const char *fn, const char *data, int length)
- {
- TRY
- {
- CFile f(fn, CFile::modeCreate | CFile::modeWrite | CFile::shareExclusive);
-
- f.Write(data, length);
- }
- CATCH(CFileException, e)
- {
- error("Unable to open %s", fn);
- }
- END_CATCH
- }
-
- void search_files(const char *dir, const char *mask, void (*callback)(const char *fn, const char *name))
- {
- CFileFind dirs, files;
- CString tmp(dir);
- BOOL ok;
-
- // First search subdirectories
-
- ok = dirs.FindFile(tmp + "\\*.*");
-
- while (ok)
- {
- ok = dirs.FindNextFile();
-
- if (dirs.IsDirectory() && !dirs.IsDots())
- search_files((LPCSTR)dirs.GetFilePath(), mask, callback);
- }
-
- // Next find files
-
- ok = files.FindFile(tmp + "\\" + mask);
-
- while (ok)
- {
- ok = files.FindNextFile();
-
- callback((LPCSTR)files.GetFilePath(), (LPCSTR)files.GetFileTitle());
- }
- }
-
- int file_exists(const char *fn)
- {
- CFile f;
-
- return f.Open(fn, CFile::modeRead | CFile::shareDenyNone);
- }